package com.wslds.blog.action.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.action.AbstractCRUDAction;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.SessionUtil;
import com.wslds.bean.Tag;
import com.wslds.bean.ext.TaginstExt;
import com.wslds.blog.service.TagService;

@SuppressWarnings("serial")
@Component("tagAdminAction")
@Scope("prototype")
public class TagAction extends AbstractCRUDAction<Tag, TagService> {

	public String refreshTagCache() {

		TaginstExt tagParam = new TaginstExt();
		List<TaginstExt> taginstExt = service.findExt("tag_select_ext",
				tagParam);
		Map<Long, List<TaginstExt>> tagMap = new HashMap<Long, List<TaginstExt>>();
		for (TaginstExt taginst : taginstExt) {
			if (tagMap.containsKey(taginst.getArtId())) {
				List<TaginstExt> list = tagMap.get(taginst.getArtId());
				list.add(taginst);
			} else {
				List<TaginstExt> list = new ArrayList<TaginstExt>();
				list.add(taginst);
				tagMap.put(taginst.getArtId(), list);
			}
		}
		SessionUtil.getServletContext().setAttribute(
				BlogCons.APPLICATION.TAGINST, tagMap);

		return SUCCESS;
	}

	public String insert() {
		String result = super.insert();
		SessionUtil.refreshTag();
		return result;
	}

	public String update() {
		String result = super.update();
		SessionUtil.refreshTag();
		return result;
	}

	@Override
	@Resource
	public void setService(TagService service) {
		this.service = service;
	}

}
